import { Component, OnInit, Input, Injector } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ModalComponentBase } from '@shared/component-base';
import { OrganizationUnits } from 'entities/organizationunits';
import { OrganizationUnitsServiceProxy } from 'services/organizationunits/organizationunits.service';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'add-users',
  templateUrl: './add-users.component.html',
  styles: [],
  providers: [OrganizationUnitsServiceProxy],
})

export class AddUsersComponent extends ModalComponentBase implements OnInit {
  @Input() unitId?: number;
  constructor(injector: Injector, private organizationunitsService: OrganizationUnitsServiceProxy) {
    super(injector);
  }
  title: string;
  tableLoading: boolean = false;
  queryFindUsers: any = {
    filter: "",
    pageIndex: 1,
    pageSize: 10,
    skipCount: function () { return (this.pageIndex - 1) * this.pageSize; },
    total: 0,
    list: [],
  };
  isSelectedAll: boolean = false; //是否全选
  checkboxCount: number = 0; //所有Checkbox数量
  checkedLength: number = 0; //已选中的数量

  ngOnInit() {
    this.title = "选择用户";
    this.getFindUsers();
  }
  getFindUsers() {
    this.resetCheckBox();
    let params: any = {};
    params.filter = this.queryFindUsers.filter;
    params.maxResultCount = this.queryFindUsers.pageSize;
    params.organizationUnitId = Number(this.unitId);
    params.skipCount = this.queryFindUsers.skipCount();
    this.tableLoading = true;
    this.organizationunitsService.findUsers(params).finally(() => {
      this.tableLoading = false;
    }).subscribe(result => {
      this.queryFindUsers.list = result.items;
      this.queryFindUsers.total = result.totalCount;
    });
  }

  save(): void {
    let arr = Array<number>();
    //获取选中的数据源push到数组
    let dataSet = this.queryFindUsers.list.filter(d => d.roleSelected == true);
    dataSet.forEach(s => {
      arr.push(Number(s.value));
    });

    this.organizationunitsService.addUsersToOrganizationUnit(this.unitId, arr)
      .pipe(finalize(() => this.saving = false))
      .subscribe((result: any) => {
        this.notify.success('保存成功！');
        this.success();
      });
  }
  //重置选择框
  resetCheckBox() {
    this.isSelectedAll = false; // 是否全选
    this.checkboxCount = 0; // 所有Checkbox数量
    this.checkedLength = 0; // 已选中的数量
  }
  //选择所有
  checkAll(e) {
    let v = this.isSelectedAll;
    this.queryFindUsers.list.forEach(u => {
      u.roleSelected = v;
    });
    if (this.isSelectedAll == false) {
      this.checkedLength = 0;
    } else {
      this.checkedLength = this.queryFindUsers.list.filter(v => v.roleSelected).length;
    }
  }
  //是否取消全选
  isCancelCheck(x: any) {
    this.checkedLength = this.queryFindUsers.list.filter(v => v.roleSelected).length;
    this.checkboxCount = this.queryFindUsers.list.length;
    if (this.checkboxCount - this.checkedLength > 0) {
      this.isSelectedAll = false;
    } else {
      this.isSelectedAll = true;
    }
  }


}
